﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using InnovaApp.Portals.MVC4Portal.Models;
using INNOVACALL.Framework.Common.MVCHelpers;
using InnovaApp.Layers.DataLayer;
using InnovaApp.Layers.ResourcesLayer.Properties;
using InnovaApp.Layers.DomainLayer;
using INNOVACALL.Framework.Common.CodeFormatting;
using INNOVACALL.Framework.Common;
using INNOVACALL.Framework.Common.Models;

namespace InnovaApp.Portals.MVC4Portal.Controllers
{
	public class Sample_SingleControl_DropDownListController : _Controllers_Base<DropDownListModel>
	{
		protected override void Initialize(System.Web.Routing.RequestContext requestContext)
		{
			base.eMethodBase = "InnovaApp.Portals.InnovaAppFrontOfficePortal.Controllers.Sample_SingleControl_DropDownListController";
			base.specificCssCollection = new string[] { "_FormCommon.css", "Views/Sample_SingleControl_DropDownList.css", "ClientComponents/CodeFormatting/csharp.css", "ClientComponents/KendoUI/kendo.common.min.css", "ClientComponents/KendoUI/kendo.blueopal.min.css" };
			base.specificJSCollection = null;
			base.dynamicJSCollection = new string[] { "/ScriptsG/inoSample_SingleControl_DropDownListDynamic.js" };
			base.specificJqueryJSCollection = new string[] { "ClientComponents/KendoUI/kendo.web.min.js", "InnovacallJS/Sample_SingleControl_DropDownList/Sample_SingleControl_DropDownList.js" };
            base.jsVariables = null;
			base.externalCSS = null;
			base.Meta.Add(enMeta.Description, Resources.View_Sample_SingleControl_DropDownList_Meta_Description);
			base.Meta.Add(enMeta.Keywords, Resources.View_Sample_SingleControl_DropDownList_Meta_Keywords);
			base.Meta.Add(enMeta.Subject, Resources.View_Sample_SingleControl_DropDownList_Meta_Subject);

			base.Initialize(requestContext);
		}

		[HttpGet, CacheManager(enCacheMode.NoCache)] //Manage your cache -- Authorize, Removed for search engine indexing
		public ActionResult Index()
		{
			//NEVER USES VIEW BAG AGAIN !
			//ViewBag.Message = "Modify this template to kick-start your ASP.NET MVC application.";
			using (DataRepositories _dataContext = new DataRepositories())
			{
				base.InitView(
					Resources.View_Sample_SingleControl_DropDownList_PageTitle,
					Resources.View_Sample_SingleControl_DropDownList_PageMessage,
					specificCssCollection,
					specificJSCollection,
					dynamicJSCollection,
					specificJqueryJSCollection,
                    jsVariables,
					externalCSS,
					Meta,
					_dataContext,
					true
					);

				ContentData.MyList = _dataContext.Lookup_Rep.GetAll().OrderBy(x => x.SortIndex).Select(x => new SelectListItem() { Value = x.Id.ToString(), Text = x.Name, Selected = false }).ToList();
				ContentData.MyList.Insert(0, new SelectListItem() { Value = "-99", Text = "Generate error", Selected = false });
				ContentData.MyList.Insert(0, new SelectListItem() { Value = "99", Text = "Generate not found", Selected = false });
				ContentData.MyList.Insert(0, new SelectListItem() { Value = "-1", Text = string.Empty, Selected = true });

				#region Involved code rendering
				INNOVACALL.Framework.Common.Models.MetaModels.CodeFile_MetaModel InvolvedSourceCode;
				INNOVACALL.Framework.Common.Models.MetaModels.CodeFile_MetaModel InvolvedSubSourceCode;
				ContentData.InvolvedCode = new InvolvedCodeModel();
				InvolvedSourceCode = new INNOVACALL.Framework.Common.Models.MetaModels.CodeFile_MetaModel();

				#region Views, partials & template editors
				InvolvedSourceCode.TabStripName = "Views, partials & template editors";
				InvolvedSourceCode.TabStripContent = string.Empty;
				InvolvedSourceCode.Language = enCodeLanguage._MultipleCodes;

				InvolvedSubSourceCode = new INNOVACALL.Framework.Common.Models.MetaModels.CodeFile_MetaModel() { Language = enCodeLanguage.Html, TabStripName = "View", TabStripContent = "/Views/Sample_SingleControl_DropDownList/Index.cshtml" };
				InvolvedSourceCode.SubCodes.Add(InvolvedSubSourceCode);

				InvolvedSubSourceCode = new INNOVACALL.Framework.Common.Models.MetaModels.CodeFile_MetaModel() { Language = enCodeLanguage.Html, TabStripName = "Layout", TabStripContent = "/Views/Shared/_Layout.cshtml" };
				InvolvedSourceCode.SubCodes.Add(InvolvedSubSourceCode);


				ContentData.InvolvedCode.Add(InvolvedSourceCode);
				#endregion

				ContentData.InvolvedCode.Add(new INNOVACALL.Framework.Common.Models.MetaModels.CodeFile_MetaModel() { Language = enCodeLanguage.cSharp, TabStripName = "Controller", TabStripContent = "/Controllers/Sample_SingleControl_DropDownListController.cs" });

				ContentData.InvolvedCode.Add(new INNOVACALL.Framework.Common.Models.MetaModels.CodeFile_MetaModel() { Language = enCodeLanguage.cSharp, TabStripName = "Model", TabStripContent = "/Models/DropDownListModel.cs" });

				#region Javascripts
				InvolvedSourceCode = new INNOVACALL.Framework.Common.Models.MetaModels.CodeFile_MetaModel();
				InvolvedSourceCode.TabStripName = "Javascripts";
				InvolvedSourceCode.TabStripContent = string.Empty;
				InvolvedSourceCode.Language = enCodeLanguage._MultipleCodes;
				InvolvedSourceCode.SubCodes.Add(new INNOVACALL.Framework.Common.Models.MetaModels.CodeFile_MetaModel() { Language = enCodeLanguage.Javascript, TabStripName = "View JS", TabStripContent = "/Scripts/InnovacallJS/Sample_SingleControl_DropDownList/Sample_SingleControl_DropDownList.js" });
				InvolvedSourceCode.SubCodes.Add(new INNOVACALL.Framework.Common.Models.MetaModels.CodeFile_MetaModel() { Language = enCodeLanguage.Javascript, TabStripName = "Layout JS", TabStripContent = "/Scripts/InnovacallJS/_Layout/_Layout.js" });
				ContentData.InvolvedCode.Add(InvolvedSourceCode);
				#endregion

				#region CSS
				InvolvedSourceCode = new INNOVACALL.Framework.Common.Models.MetaModels.CodeFile_MetaModel();
				InvolvedSourceCode.TabStripName = "CSS";
				InvolvedSourceCode.TabStripContent = string.Empty;
				InvolvedSourceCode.Language = enCodeLanguage._MultipleCodes;
				InvolvedSourceCode.SubCodes.Add(new INNOVACALL.Framework.Common.Models.MetaModels.CodeFile_MetaModel() { Language = enCodeLanguage.Css, TabStripName = "View CSS", TabStripContent = "/Content/Views/Sample_SingleControl_DropDownList.css" });
				InvolvedSourceCode.SubCodes.Add(new INNOVACALL.Framework.Common.Models.MetaModels.CodeFile_MetaModel() { Language = enCodeLanguage.Css, TabStripName = "Form Common CSS", TabStripContent = "/Content/_FormCommon.css" });
				InvolvedSourceCode.SubCodes.Add(new INNOVACALL.Framework.Common.Models.MetaModels.CodeFile_MetaModel() { Language = enCodeLanguage.Css, TabStripName = "Site (Layout) CSS", TabStripContent = "/Content/_Site.css" });
				ContentData.InvolvedCode.Add(InvolvedSourceCode);
				#endregion

				#region Generated client files
				InvolvedSourceCode = new INNOVACALL.Framework.Common.Models.MetaModels.CodeFile_MetaModel();
				InvolvedSourceCode.TabStripName = "Generated client files";
				InvolvedSourceCode.TabStripContent = string.Empty;
				InvolvedSourceCode.Language = enCodeLanguage._MultipleCodes;

				InvolvedSubSourceCode = new INNOVACALL.Framework.Common.Models.MetaModels.CodeFile_MetaModel();
				InvolvedSubSourceCode.TabStripName = "Javascript";
				InvolvedSubSourceCode.TabStripContent = string.Empty;
				InvolvedSubSourceCode.SubCodes.Add(new INNOVACALL.Framework.Common.Models.MetaModels.CodeFile_MetaModel() { Language = enCodeLanguage.cSharp, TabStripName = "Controller", TabStripContent = "/Controllers/ScriptsGController.cs" });
				InvolvedSubSourceCode.SubCodes.Add(new INNOVACALL.Framework.Common.Models.MetaModels.CodeFile_MetaModel() { Language = enCodeLanguage.Html, TabStripName = "View", TabStripContent = "/Views/ScriptsG/inoSample_SingleControl_DropDownListDynamic.cshtml" });
				InvolvedSubSourceCode.SubCodes.Add(new INNOVACALL.Framework.Common.Models.MetaModels.CodeFile_MetaModel() { Language = enCodeLanguage.GeneratedJavascript, TabStripName = "Generated javascript", TabStripContent = "/ScriptsG/inoSample_SingleControl_DropDownListDynamic.js" });
				InvolvedSourceCode.SubCodes.Add(InvolvedSubSourceCode);

				InvolvedSubSourceCode = new INNOVACALL.Framework.Common.Models.MetaModels.CodeFile_MetaModel();
				InvolvedSubSourceCode.TabStripName = "CSS";
				InvolvedSubSourceCode.TabStripContent = string.Empty;
				InvolvedSubSourceCode.SubCodes.Add(new INNOVACALL.Framework.Common.Models.MetaModels.CodeFile_MetaModel() { Language = enCodeLanguage.cSharp, TabStripName = "Controller", TabStripContent = "/Controllers/CSSGController.cs" });
				InvolvedSubSourceCode.SubCodes.Add(new INNOVACALL.Framework.Common.Models.MetaModels.CodeFile_MetaModel() { Language = enCodeLanguage.Html, TabStripName = "View", TabStripContent = "/Views/CSSG/site.cshtml" });
				InvolvedSubSourceCode.SubCodes.Add(new INNOVACALL.Framework.Common.Models.MetaModels.CodeFile_MetaModel() { Language = enCodeLanguage.GeneratedCss, TabStripName = "Generated CSS (from _site.css)", TabStripContent = "/CSSG/Site.css" });
				InvolvedSourceCode.SubCodes.Add(InvolvedSubSourceCode);

				ContentData.InvolvedCode.Add(InvolvedSourceCode);
				#endregion

				RenderCodeFiles(ContentData.InvolvedCode);
				#endregion
			}
			return View(ContentData);
		}

		/// <summary>
		/// Error is catched automatically
		/// </summary>
		/// <param name="Id"></param>
		/// <returns></returns>
		[HttpPost, CacheManager(enCacheMode.NoCache)] //Manage your cache -- Authorize, Removed for search engine indexing
		public JsonResult GetItemDescription(int Id)
		{
			if(Id < 0 ) throw new Exception("Error");
			JsonResult toReturn = new JsonResult { JsonRequestBehavior = JsonRequestBehavior.AllowGet };
			string htmlResult;
			using (DataRepositories _dataContext = new DataRepositories())
			{
				Lookup dbLookup = _dataContext.Lookup_Rep.Query(x => x.Id == Id).FirstOrDefault();
				if (dbLookup != null)
				{
					htmlResult = Server.HtmlEncode(dbLookup.Description);
					toReturn.Data = new { @success = true, @divToUpdate = "#divDescription", @htmlResult = htmlResult };
				}
				else
				{
					htmlResult = Server.HtmlEncode(Resources.View_Sample_SingleControl_DropDownList_ItemNotFound);
					toReturn.Data = new { @success = false, @divToUpdate = "#divDescription", @htmlResult = htmlResult };
				}
			}
			return toReturn;
		}
	}
}
